<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库单号" prop="outId">
        <el-input
          v-model="queryParams.outId"
          placeholder="请输入出库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联单号" prop="evidenceRecordId">
        <el-input
          v-model="queryParams.evidenceRecordId"
          placeholder="请输入关联单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库状态" prop="outcomeState">
        <el-input
          v-model="queryParams.outcomeState"
          placeholder="请输入出库状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['storage:exitRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exitRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出库单号" align="center" prop="outId" />
      <el-table-column label="关联单号" align="center" prop="evidenceRecordId" />
      <el-table-column label="出库类型" align="center" prop="outcomeType" />
      <el-table-column label="出库状态" align="center" prop="outcomeState" />
      <el-table-column label="出库日期" align="center" prop="outcomeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outcomeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="generatedBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['storage:exitRecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['storage:exitRecord:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listExitRecord, getExitRecord, delExitRecord, addExitRecord, updateExitRecord } from "@/api/storage/exitRecord";

export default {
  name: "ExitRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedExitDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库表格数据
      exitRecordList: [],
      // 出库详情表格数据
      exitDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outId: null,
        warehouseId: null,
        evidenceRecordId: null,
        outcomeType: null,
        outcomeState: null,
        generatedBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        evidenceRecordId: [
          { required: true, message: "关联单号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出库列表 */
    getList() {
      this.loading = true;
      listExitRecord(this.queryParams).then(response => {
        this.exitRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        outId: null,
        warehouseId: null,
        evidenceRecordId: null,
        outcomeType: null,
        outcomeState: null,
        outcomeDate: null,
        generatedBy: null
      };
      this.exitDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.outId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const outId = row.outId || this.ids
      getExitRecord(outId).then(response => {
        this.form = response.data;
        this.exitDetailList = response.data.exitDetailList;
        this.open = true;
        this.title = "修改出库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.exitDetailList = this.exitDetailList;
          if (this.form.outId != null) {
            updateExitRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExitRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const outIds = row.outId || this.ids;
      this.$modal.confirm('是否确认删除出库编号为"' + outIds + '"的数据项？').then(function() {
        return delExitRecord(outIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 出库详情序号 */
    rowExitDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 出库详情添加按钮操作 */
    handleAddExitDetail() {
      let obj = {};
      obj.batchId = "";
      obj.exitType = "";
      obj.origin = "";
      obj.goodSpecification = "";
      obj.merchandiseKind = "";
      obj.manufactureDate = "";
      obj.areaId = "";
      obj.location = "";
      this.exitDetailList.push(obj);
    },
    /** 出库详情删除按钮操作 */
    handleDeleteExitDetail() {
      if (this.checkedExitDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的出库详情数据");
      } else {
        const exitDetailList = this.exitDetailList;
        const checkedExitDetail = this.checkedExitDetail;
        this.exitDetailList = exitDetailList.filter(function(item) {
          return checkedExitDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleExitDetailSelectionChange(selection) {
      this.checkedExitDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/exitRecord/export', {
        ...this.queryParams
      }, `exitRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
